import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Menu extends JFrame implements ActionListener{

    private Toolkit toolkit;
   //static int i = 0;
    JButton[] t = new JButton[16];
    JButton add =new JButton("Add to order");
    JButton back =new JButton("Back");
    JTextArea orderlist =new JTextArea();
    Menud md =new Menud();
    OrderManagement orders;
    int st=0;
    int ornum=0;
    String lastOrder="";
    
    public Menu()
    {
    	;
    }
    
    public Menu(int input, OrderManagement order, int ordnumb) {
    	setTitle("Restaurant Master 2.0 - Menu");
        setSize(800, 600);
        st=input;
        orders=order;
        ornum=ordnumb;
        Menud.load("menu.txt");
        refresh();
        
        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation((size.width - getWidth())/2, (size.height - getHeight())/2);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);
        
        //orderlist.setText(orderlist.getText()+"\n"+orders.printReceipt(st));
        
        orderlist.setBounds(500,0,300,600);
		
        //instantiate buttons
		for(int j=0; j<16; j++)
	        {
	        	t[j]= new JButton(Menud.getItem(j));
	        	//System.out.println(Menud.getItem(j));
	        }
		
		
		//place buttons
		back.setBounds(25,525,100,30);
		
		t[0].setBounds(0,0,120,120);
		t[1].setBounds(120,0,120,120);
		t[2].setBounds(240,0,120,120);
		t[3].setBounds(360,0,120,120);
		
		t[4].setBounds(0,120,120,120);
		t[5].setBounds(120,120,120,120);
		t[6].setBounds(240,120,120,120);
		t[7].setBounds(360,120,120,120);
		
		t[8].setBounds(0,240,120,120);
		t[9].setBounds(120,240,120,120);
		t[10].setBounds(240,240,120,120);
		t[11].setBounds(360,240,120,120);
		
		t[12].setBounds(0,360,120,120);
		t[13].setBounds(120,360,120,120);
		t[14].setBounds(240,360,120,120);
		t[15].setBounds(360,360,120,120);
		
		//add action listeners to buttons
		for (int l=0;l<16;l++)
			{
				t[l].addActionListener(this);
			}
		back.addActionListener(this);
		
		
		//add to panel
		for(int j=0; j<16; j++)
		{
			panel.add(t[j]);
		}
		panel.add(back);
		panel.add(orderlist);
		
	
    }
    
    //event handler
    

    
    public void actionPerformed(ActionEvent event)
    {
    	Object source = event.getSource();
    	//refresh();
    	
    	if(source==back)
    	{
    		dispose();
    	}
    	
    	if(source==t[0])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[0].getText());
    		//lastOrder=t[0].getText();
    		refresh();
    	}
    	
    	if(source==t[1])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[1].getText());
    		//lastOrder=t[1].getText();
    		refresh();
    	}
    	
    	if(source==t[2])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[2].getText());
    		//lastOrder=t[2].getText();
    		refresh();
    	}
    	
    	if(source==t[3])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[3].getText());
    		//lastOrder=t[3].getText();
    		refresh();
    	}
    	
    	if(source==t[4])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[4].getText());
    		//lastOrder=t[4].getText();
    		refresh();
    	}
    	
    	if(source==t[5])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[5].getText());
    		//lastOrder=t[5].getText();
    		refresh();
    	}
    	
    	if(source==t[6])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[6].getText());
    		//lastOrder=t[6].getText();
    		refresh();
    	}
    	
    	if(source==t[7])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[7].getText());
    		//lastOrder=t[7].getText();
    		refresh();
    	}
    	
    	if(source==t[8])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[8].getText());
    		//lastOrder=t[8].getText();
    		refresh();
    	}
    	
    	if(source==t[9])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[9].getText());
    		//lastOrder=t[9].getText();
    		refresh();
    	}
    	
    	if(source==t[10])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[10].getText());
    		//lastOrder=t[10].getText();
    		refresh();
    	}
    	
    	if(source==t[11])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[11].getText());
    		//lastOrder=t[11].getText();
    		refresh();
    	}
    	
    	if(source==t[12])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[12].getText());
    		//lastOrder=t[12].getText();
    		refresh();
    	}
    	
    	if(source==t[13])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[13].getText());
    		refresh();
    	}
    	
    	if(source==t[14])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[14].getText());
    		refresh();
    	}
    	
    	if(source==t[15])
    	{
    		lastOrder=orders.addItemToOrder(ornum, t[15].getText());
    		refresh();
    	}

    }
    
    public void refresh()
    {
    	orderlist.setText(orderlist.getText()+"\n"+lastOrder);
    }
    
    }